package com.example.blog.controller;

import com.example.blog.dto.UserAdminDTO;
import com.example.blog.service.UserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/users")
@CrossOrigin(origins = "*", maxAge = 3600)
@PreAuthorize("hasRole('ADMIN')")
public class UserAdminController {
    @Autowired
    private UserAdminService userAdminService;

    @GetMapping
    public ResponseEntity<Page<UserAdminDTO>> getAllUsers(
            @RequestParam(required = false) String keyword,
            Pageable pageable) {
        return ResponseEntity.ok(userAdminService.getAllUsers(keyword, pageable));
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserAdminDTO> getUserById(@PathVariable Long id) {
        return ResponseEntity.ok(userAdminService.getUserById(id));
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserAdminDTO> updateUser(
            @PathVariable Long id,
            @RequestBody UserAdminDTO userDTO) {
        return ResponseEntity.ok(userAdminService.updateUser(id, userDTO));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteUser(@PathVariable Long id) {
        userAdminService.deleteUser(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/enable")
    public ResponseEntity<?> enableUser(@PathVariable Long id) {
        userAdminService.enableUser(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/disable")
    public ResponseEntity<?> disableUser(@PathVariable Long id) {
        userAdminService.disableUser(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/grant-admin")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    public ResponseEntity<?> grantAdminRole(@PathVariable Long id) {
        userAdminService.grantAdminRole(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/revoke-admin")
    @PreAuthorize("hasRole('SUPER_ADMIN')")
    public ResponseEntity<?> revokeAdminRole(@PathVariable Long id) {
        userAdminService.revokeAdminRole(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/reset-password")
    public ResponseEntity<?> resetPassword(@PathVariable Long id) {
        userAdminService.resetPassword(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/inactive")
    public ResponseEntity<Page<UserAdminDTO>> getInactiveUsers(
            @RequestParam(defaultValue = "30") int days,
            Pageable pageable) {
        return ResponseEntity.ok(userAdminService.getInactiveUsers(days, pageable));
    }

    @GetMapping("/new")
    public ResponseEntity<Page<UserAdminDTO>> getNewUsers(
            @RequestParam(defaultValue = "7") int days,
            Pageable pageable) {
        return ResponseEntity.ok(userAdminService.getNewUsers(days, pageable));
    }
} 